package com.example.shaidemo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SavePicture {
    public String SaveScreenShot(Bitmap bitmap, Activity activity){
        Uri uri = null;
        String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
        OutputStream outStream = null;
        //以保存时间为文件名
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmss");
        String image_save_path =  sdf.format(date);

        File file = new File(extStorageDirectory, image_save_path+".JPEG");//创建文件，第一个参数为路径，第二个参数为文件名
        String picturePath=file.getPath();

        try {
            outStream = new FileOutputStream(file);//创建输入流
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();
//       这三行可以实现相册更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            uri = Uri.fromFile(file);intent.setData(uri);
            activity.sendBroadcast(intent);


        } catch(Exception e) {
//            Toast.makeText(activity, "exception:" + e, Toast.LENGTH_SHORT).show();
        }
        return picturePath;
    }
}
